package com.derbysoft.dswitch.adapter.ihg.web.action.admin.setting.channel;

import com.derbysoft.dswitch.adapter.ihg.common.domain.ChannelAuthorization;
import com.derbysoft.dswitch.adapter.ihg.common.domain.RatePlan;
import com.derbysoft.dswitch.adapter.ihg.common.repository.ChannelAuthorizationRepository;
import com.derbysoft.dswitch.adapter.ihg.common.repository.RatePlanRepository;
import com.derbysoft.dswitch.adapter.ihg.web.action.support.BaseAction;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * Created by: jason
 * Date: 2012-06-11
 */
public class EditAction extends BaseAction {

    @Autowired
    private ChannelAuthorizationRepository channelAuthorizationRepository;

    @Autowired
    private RatePlanRepository ratePlanRepository;

    private String channelCode;

    private List<String> ratePlanCodes;

    private ChannelAuthorization channel;

    private List<RatePlan> ratePlanList;

    @Action("edit")
    public String edit() {
        if (StringUtils.isBlank(channelCode)) {
            return LIST;
        }
        channel = channelAuthorizationRepository.loadByDerbyChannelCode(channelCode);
        if (channel == null) {
            return LIST;
        }
        ratePlanList = ratePlanRepository.loadAll();
        return EDIT;
    }

    @Action("update")
    public String update() {
        ChannelAuthorization exist = channelAuthorizationRepository.load(channel.getId());
        if (exist == null) {
            return LIST;
        }
        exist.setIataNumber(channel.getIataNumber());
        exist.setUserName(channel.getUserName());
        exist.setPassword(channel.getPassword());
        exist.getRatePlans().clear();
        if (CollectionUtils.isNotEmpty(ratePlanCodes)) {
            for (String ratePlanCode : ratePlanCodes) {
                exist.getRatePlans().add(ratePlanRepository.loadByCode(ratePlanCode.trim()));
            }
        }
        channelAuthorizationRepository.save(exist);
        return LIST;
    }

    @Action("gotoAdd")
    public String emptyAdd() {
        ratePlanList = ratePlanRepository.loadAll();
        return ADD;
    }

    @Action("add")
    public String add() {
        if (channel != null) {
            if (channelAuthorizationRepository.loadByDerbyChannelCode(channel.getDerbyChannelCode()) != null) {
                return LIST;
            }
            if (CollectionUtils.isNotEmpty(ratePlanCodes)) {
                for (String ratePlanCode : ratePlanCodes) {
                    channel.getRatePlans().add(ratePlanRepository.loadByCode(ratePlanCode.trim()));
                }
            }
            channelAuthorizationRepository.save(channel);
        }
        return LIST;
    }

    @Action("delete")
    public String delete() {
        if (channelCode != null) {
            ChannelAuthorization exist = channelAuthorizationRepository.loadByDerbyChannelCode(channelCode);
            if (exist != null) {
                channelAuthorizationRepository.delete(exist);
            }
        }
        return LIST;
    }

    public void setChannelCode(String channelCode) {
        this.channelCode = channelCode;
    }

    public List<String> getRatePlanCodes() {
        return ratePlanCodes;
    }

    public void setRatePlanCodes(List<String> ratePlanCodes) {
        this.ratePlanCodes = ratePlanCodes;
    }

    public ChannelAuthorization getChannel() {
        return channel;
    }

    public void setChannel(ChannelAuthorization channel) {
        this.channel = channel;
    }

    public List<RatePlan> getRatePlanList() {
        return ratePlanList;
    }
}
